package org.gry.dy20180621;

import java.io.*;
import java.net.*;
import java.util.HashMap;
import java.util.Map;

public class TestGet {

	public static void main(String args[]) {
		TestGet.sendGet("http://192.168.0.111:8081/hello");// 发送GET请求
	}

	public static void sendGet(String url) {
		StringBuilder sb = new StringBuilder();
		BufferedReader in = null;
		try {
			URL realUrl = new URL("http", "192.168.0.111", 8081, "/hello");
			InputStream openStream = realUrl.openStream();
			String line;
			BufferedReader br = new BufferedReader(new InputStreamReader(openStream));
			while ((line = br.readLine()) != null) {
				sb.append(line);
			}
			String str = sb.toString();
			String text = str.trim();
			String text1 = text.substring(1, text.length() - 1);
			String[] split1 = text1.split(",");
			User user = new User();
			User user1 = new User();
			for (String a : split1) {
				String[] split2 = a.split(":");
				String q = split2[0].substring(1, split2[0].length() - 1);
				if (q.equals("id")) {
					user.setId(Integer.parseInt(split2[1]));
				} else {
					user.setUserName(split2[1].replace("\"", ""));
				}
			}
			Map<String, String> mp = new HashMap<>();
			for (String a : split1) {
				String[] split2 = a.split(":");
				String q = split2[0].substring(1, split2[0].length() - 1);
				mp.put(q, split2[1].replace("\"", ""));
			}
			user1.setId(Integer.parseInt(mp.get("id")));
			user1.setUserName(mp.get("username"));
			user.toString();
			user1.toString();
		} catch (Exception e) {
			System.out.println("发送GET请求出现异常！" + e);
			e.printStackTrace();
		}
		// 使用finally块来关闭输入流
		finally {
			try {
				if (in != null) {
					in.close();
				}
			} catch (IOException ex) {
				ex.printStackTrace();
			}
		}
	}
}
